<?php
// +----------------------------------------------------------------------
// | 异步产品数据处理
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/06/09
// +----------------------------------------------------------------------
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/common/func.inc.php';
include_once '/var/www/html/new/d/class/Alliance.class.php';
include_once '/var/www/html/new/d/class/abc/Validate.class.php';
include_once '/var/www/html/new/d/class/abc/Product.class.php';
include_once '/var/www/html/new/d/class/abc/Syslog.class.php';

if(!$_SESSION['memberID']) exit('{"status":"fail","msg":"登录超时请重新登录","code":0}');
if(!isset($_REQUEST['action'])) exit('{"status":"fail","msg":"参数错误","code":101}');


$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
// sleep(2);
// $mt = mt_rand(0,1);
// if($mt==0) exit('{"status":"fail","msg":"参数错误","code":101}');
// exit('{"status":"success","msg":"成功","code":200}');

switch(trim($_REQUEST['action'])){
	
	// 关闭转供应
	case 'turnoffEvo':
		// exit;
		$pid = $_REQUEST['pid']+0;// 配置的产品ID
		$sid = $_SESSION['sid'];// 会员ID
		$now_aid = $_REQUEST['supplier']+0;// 关闭的上级供应商ID
		$res_aid = $_REQUEST['rsup']+0;// 原开启转供应的上级供应ID
		
		// 1.关闭转供应
		$sql = "select id from pft_p_apply_evolute where pid=$pid and fid=$sid and active=1 and sid=$res_aid limit 1";
		$GLOBALS['le']->query($sql);
		if(!$GLOBALS['le']->fetch_assoc()) exit('{"status":"fail","msg":"供应链不存在","code":202}');
		
		$eid = $GLOBALS['le']->f('id');
		$upd = "update pft_p_apply_evolute set active=0 where id=$eid and pid=$pid and fid=$sid and sid=$res_aid limit 1";
		// abc777\Syslog::write_log($upd,'sql');
		// write_log($upd,'sql');
		if(!$GLOBALS['le']->query($upd))  exit('{"status":"fail","msg":"关闭失败","code":201}');
		
		// 2.断开经该供应商转分销下去的链
		$evolist = _RecvEvoluteRow($pid, $sid);
		if(count($evolist)){
			$ch_upd_ids = array();
			$detect = array();
			$detect_data = array();
			foreach($evolist as $key=>$row){
				if($row['status']==0) $ch_upd_ids[] = $row['id'];
			}
			if(count($ch_upd_ids)){
				$sql = "update pft_p_apply_evolute set status=1 where id in (".implode(',', $ch_upd_ids).") and pid=$pid limit ".count($ch_upd_ids);
				// abc777\Syslog::write_log($sql,'sql');
				// write_log($sql,'sql');
				$GLOBALS['le']->query($sql);
			}
		}		
		exit('{"status":"success","msg":"关闭成功","code":200}');
		break;
	// 更改转供应
	case 'setEvoChain':
		// exit;
		$pid = $_REQUEST['pid'] + 0;// 配置的产品ID
		$sid = $_SESSION['sid'];// 会员ID
		$now_aid = $_REQUEST['supplier'] + 0;// 上级供应商ID
		$res_aid = $_REQUEST['rsup'] + 0;// 原开启转供应的上级供应ID
		// write_log(json_encode($_REQUEST), 'sql');
		
		// 1 将已开启的转供应的关闭
		$ch_aids_f = false;
		if($res_aid>0){
			$sql = "select id,aids from pft_p_apply_evolute where pid=$pid and fid=$sid and active=1 and sid=$res_aid limit 1";
			$GLOBALS['le']->query($sql);
			if($GLOBALS['le']->fetch_assoc()){
				$eid    = $GLOBALS['le']->f('id');
				$aids_o = $GLOBALS['le']->f('aids');// 旧供应链
				$upd    = "update pft_p_apply_evolute set active=0 where id=$eid and pid=$pid and fid=$sid and sid=$res_aid limit 1";
				// abc777\Syslog::write_log($upd,'sql');
				// write_log($upd,'sql');
				if(!$GLOBALS['le']->query($upd)) exit('{"status":"fail","msg":"已开启的转供应链的关闭错误","code":201}');
				$ch_aids_f = true;// 需改变 aids 标识
			}
		}
		
		// 2 开启指定的转供应
		$sql = "select id,aids from pft_p_apply_evolute where pid=$pid and fid=$sid and sid=$now_aid and status=0 and active=0 limit 1";
		$GLOBALS['le']->query($sql);
		if(!$GLOBALS['le']->fetch_assoc()){// 不存在分销链，直销产品
			$sql = "insert into pft_p_apply_evolute set pid=$pid,fid=$sid,sid=$now_aid,sourceid=$now_aid,aids=$now_aid,lvl=1,active=1,rectime=now()";
			if(!$GLOBALS['le']->query($sql)) exit('{"status":"fail","msg":"开启转供应失败","code":500}');
			$aids_n = $now_aid;
		}else{
			$eid    = $GLOBALS['le']->f('id');
			$aids_n = $GLOBALS['le']->f('aids');// 新供应链的 aids
			$upd  = "update pft_p_apply_evolute set active=1 where id=$eid and sid=$now_aid and pid=$pid and fid=$sid limit 1";
			if(!$GLOBALS['le']->query($upd)) exit('{"status":"fail","msg":"转换失败","code":203}');
		}
		
		// 3 切换子节点链的aids
		if($ch_aids_f){
			
			$detect = array();
			$detect_data = array();
			$box_arr = array();// 最后需要更改的数组
			$evolist = _RecvEvoluteRow($pid, $sid);// 递归获取树形
			// print_r($evolist);
			foreach($evolist as $key=>$row){
				if(strpos($row['aids'], $aids_o)===0){
					$aids_arr = explode(',', $row['aids']);
					$aids_key = array_search($sid, $aids_arr);
					if($aids_key===false) continue;
					$aids_arr = array_splice($aids_arr, $aids_key);
					array_unshift($aids_arr, $aids_n);
					$new_aids = implode(',', $aids_arr);
					$new_lvl  = substr_count($new_aids, ',') + 1;
					$upd = "update pft_p_apply_evolute set aids='$new_aids',lvl=$new_lvl where id={$row['id']} and pid=$pid and sid={$row['sid']} limit 1";
					$box_arr[] = $upd;
				}
				
			}
			
			
			foreach($box_arr as $key=>$sql_udp){
				$GLOBALS['le']->query($sql_udp);
				// abc777\Syslog::write_log($sql_udp,'sql');
				// write_log($sql_udp,'sql');
			}
		}
		// 分销联盟给联盟成员配置产品
		if($ch_aids_f==false && $_SESSION['alliance']) Alliance::setEvoluteList($pid,$now_aid);
		// print_r($evolist);
		// print_r($box_arr);
		exit('{"status":"success","msg":"转换成功","code":200}');
		break;
		
	// 默认错误
	default:exit('{"status":"fail","msg":"参数错误","code":101}');break;
		
}


//  递归获取树形产品供应链
function _RecvEvoluteRow($pid, $sid, $aids=''){
	
	$container = array();
	$sql = "select * from pft_p_apply_evolute where pid=$pid and sid=$sid and status=0";
	if($aids!='') $sql = $sql." and aids='$aids'";
	// echo $sql;
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$container[] = $row;
	}
	// print_r($container);
	if(count($container)){
		foreach($container as $row){
			$container = array_merge($container, _RecvEvoluteRow($pid, $row['fid'], $row['aids'].','.$row['fid']));
		}
	}
	return $container;
}


// 调试日志
function write_log($txt, $suffix=''){
	$file = '/var/www/html/new/d/abc/logs/abc777_debug_'.$suffix.'_'.date('Ymd').'_'.$_SESSION['sid'].'.txt';
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}